package org.dxy.ai.tools;

import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.factory.CreateFactoryRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

/**
 * @ description:
 * @ author: dxy
 * @ createTime: 2025/10/8 1:49
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class FactoryTool {

    /**
     * 创建工厂
     *
     * @param factoryCode 工厂编码，必填，最大50字符
     * @param factoryName 工厂名称，必填，最大100字符
     * @param address     工厂地址，可选，最大200字符
     * @param phone       联系电话，可选，最大20字符
     * @param manager     负责人，可选，最大50字符
     * @param description 描述，可选，最大500字符
     */
    @Tool("创建工厂：根据提供的工厂编码、名称、地址、电话、负责人和描述信息来创建新工厂")
    public void createFactory(
            @NotBlank @Size(max = 50) String factoryCode,
            @NotBlank @Size(max = 100) String factoryName,
            @Size(max = 200) String address,
            @Size(max = 20) String phone,
            @Size(max = 50) String manager,
            @Size(max = 500) String description) {

        WebClient client = WebClient.builder()
                .baseUrl("http://localhost:8080/api")
                .build();

        CreateFactoryRequest request = CreateFactoryRequest.builder()
                .factoryCode(factoryCode)
                .factoryName(factoryName)
                .address(address)
                .phone(phone)
                .manager(manager)
                .description(description)
                .build();

        log.info("准备发送创建工厂请求：factoryCode={}, factoryName={}", factoryCode, factoryName);

        client.post()
                .uri("/factories/create")
                .bodyValue(request)
                .retrieve()
                .bodyToMono(String.class)
                .doOnSubscribe(sub -> log.info("请求已触发 POST /factories/create"))
                .doOnSuccess(resp -> log.info("创建工厂请求成功，响应: {}", resp))
                .doOnError(error -> log.error("创建工厂请求失败: {}", error.getMessage(), error))
                .subscribe();
    }

}
